<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>9.7. Working examples</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="prev" href="zend.date.constants.html" title="9.6. Constants for General Date Functions">
<link rel="next" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.date.additional.html#zend.date.additional.checking" title="9.7.1. Checking dates">
<link rel="subsection" href="zend.date.additional.html#zend.date.additional.sunrise-sunset" title="9.7.2. Sunrise and Sunset">
<link rel="subsection" href="zend.date.additional.html#zend.date.additional.timezones" title="9.7.3. Timezones">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">9.7. Working examples</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.date.constants.html">Prev</a> </td>
<th width="60%" align="center">Chapter 9. Zend_Date</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.db.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.date.additional"></a>9.7. Working examples</h2></div></div></div>
<p>
        Within this chapter, we will describe several additional functions which are also available through
        <code class="code">Zend_Date</code>. Of course all described functions have additional examples to show the
        expected working and the simple API for the proper using of them.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.date.additional.checking"></a>9.7.1. Checking dates</h3></div></div></div>
<p>
            Probably most dates you will get as input are strings. But the problem with strings is that
            you can not be sure if the string is a real date. Therefor <code class="code">Zend_Date</code> has spent
            an own static function to check date strings. <code class="code">Zend_Locale</code> has an own function
            <code class="code">getDate($date, $locale);</code> which parses a date and returns the proper and normalized
            date parts. A monthname for example will be recognised and returned just a month number.
            But as <code class="code">Zend_Locale</code> does not know anything about dates because it is a
            normalizing and localizing class we have integrated an own function <code class="code">isDate($date);</code>
            which checks this.
        </p>
<p>
            <code class="code">isDate($date, $format, $locale);</code> can take up to 3 parameters and needs minimum one
            parameter. So what we need to verify a date is, of course, the date itself as string.
            The second parameter can be the format which the date is expected to have. If no format is given
            the standard date format from your locale is used. For details about how formats should look like
            see the chapter about <a href="zend.date.constants.html#zend.date.constants.selfdefinedformats" title="9.6.3. Self-Defined OUTPUT Formats with ISO">self defined formats</a>
            .
        </p>
<p>
            The third parameter is also optional as the second parameter and can be used to give a locale.
            We need the locale to normalize monthnames and daynames. So with the third parameter we are able
            to recognise dates like '01.Jänner.2000' or '01.January.2000' depending on the given locale.
        </p>
<p>
            <code class="code">isDate();</code> of course checks if a date does exist. <code class="code">Zend_Date</code> itself does not
            check a date. So it is possible to create a date like '31.February.2000' with <code class="code">Zend_Date</code>
            because <code class="code">Zend_Date</code> will automatically correct the date and return the proper date.
            In our case '03.March.2000'. <code class="code">isDate()</code> on the other side does this check and will return
            false on '31.February.2000' because it knows that this date is impossible.
        </p>
<div class="example">
<a name="zend.date.additional.checking.example-1"></a><p class="title"><b>Example 9.17. Checking dates</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

// Checking dates
$date = '01.03.2000';
if (Zend_Date::isDate($date)) {
    print "String $date is a date";
} else {
    print "String $date is NO date";
}

// Checking localized dates
$date = '01 February 2000';
if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
    print "String $date is a date";
} else {
    print "String $date is NO date";
}

// Checking impossible dates
$date = '30 February 2000';
if (Zend_Date::isDate($date,'dd MMMM yyyy', 'en')) {
    print "String $date is a date";
} else {
    print "String $date is NO date";
}
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.date.additional.sunrise-sunset"></a>9.7.2. Sunrise and Sunset</h3></div></div></div>
<p>
            <code class="code">Zend_Date</code> has also functions integrated for getting informations from the sun. Often it is
            necessary to get the time for sunrise or sunset within a particularly day. This is quite easy with
            <code class="code">Zend_Date</code> as just the expected day has to be given and additionally location for which
            the sunrise or sunset has to be calculated.
        </p>
<p>
            As most people do not know the location of their city we have also spent a helper class which provides
            the location data for about 250 capital and other big cities around the whole world. Most people could
            use cities near themself as the difference for locations situated to each other can only be measured
            within some seconds.
        </p>
<p>
            For creating a listbox and choosing a special city the function <code class="code">Zend_Date_Cities::getCityList</code>
            can be used. It returns the names of all available predefined cities for the helper class.
        </p>
<div class="example">
<a name="zend.date.additional.sunrise-sunset.example-1"></a><p class="title"><b>Example 9.18. Getting all available cities</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date/Cities.php';

// Output the complete list of available cities
print_r (Zend_Date_Cities::getCityList());
            </pre></div>
</div>
<br class="example-break"><p>
            The location itself can be received with the <code class="code">Zend_Date_Cities::City()</code> function.
            It accepts the name of the city as returned by the <code class="code">Zend_Date_Cities::getCityList()</code>
            function and optional as second parameter the horizon to set.
        </p>
<p>
            There are 4 defined horizons which can be used with locations to receive the exact time of sunset and
            sunrise. The '<code class="code">horizon</code>' parameter is always optional in all functions. If it is not set, the
            '<code class="code">effective</code>' horizon is used.
        </p>
<div class="table">
<a name="zend.date.additional.sunrise-sunset.table"></a><p class="title"><b>Table 9.18. Types of supported horizons for sunset and sunrise</b></p>
<div class="table-contents"><table summary="Types of supported horizons for sunset and sunrise" border="1">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>Horizon</th>
<th>Description</th>
<th>Usage</th>
</tr></thead>
<tbody>
<tr>
<td>effective</td>
<td>Standard horizon</td>
<td>Expects the world to be a ball. This horizon is always used if non is defined.</td>
</tr>
<tr>
<td>civil</td>
<td>Common horizon</td>
<td>Often used in common medias like TV or radio</td>
</tr>
<tr>
<td>nautic</td>
<td>Nautic horizon</td>
<td>Often used in sea navigation</td>
</tr>
<tr>
<td>astronomic</td>
<td>Astronomic horizon</td>
<td>Often used for calculation with stars</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
            Of course also a self-defined location can be given and calculated with. Therefor a '<code class="code">latitude</code>'
            and a '<code class="code">longitude</code>' has to be given and optional the '<code class="code">horizon</code>'.
        </p>
<div class="example">
<a name="zend.date.additional.sunrise-sunset.example-2"></a><p class="title"><b>Example 9.19. Getting the location for a city</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date/Cities.php';

// Get the location for a defined city
// uses the effective horizon as no horizon is defined
print_r (Zend_Date_Cities::City('Vienna'));

// use the nautic horizon
print_r (Zend_Date_Cities::City('Vienna', 'nautic'));

// self definition of a location
$mylocation = array('latitude' =&gt; 41.5, 'longitude' =&gt; 13.2446);
            </pre></div>
</div>
<br class="example-break"><p>
            As now all needed data can be set the next is to create a <code class="code">Zend_Date</code> object with the day where
            sunset or sunrise should be calculated. For the calculation there are 3 functions available. It is possible
            to calculate sunset with '<code class="code">getSunset()</code>', sunrise with '<code class="code">getSunrise()</code>' and all
            available informations related to the sun with '<code class="code">getSunInfo()</code>'. After the calculation the
            <code class="code">Zend_Date</code> object will be returned with the calculated time.
        </p>
<div class="example">
<a name="zend.date.additional.sunrise-sunset.example-3"></a><p class="title"><b>Example 9.20. Calculating sun informations</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';
require_once 'Zend/Date/Cities.php';

// Get the location for a defined city
$city = Zend_Date_Cities::City('Vienna');

// create a date object for the day for which the sun has to be calculated
$date = new Zend_Date('10.03.2007', Zend_Date::ISO_8601, 'de');

// calculate sunset
$sunset = $date-&gt;getSunset($city);
print $sunset-&gt;get(Zend_Date::ISO_8601);

// calculate all sun informations
$info = $date-&gt;getSunInfo($city);
foreach ($info as $sun) {
    print "\n" . $sun-&gt;get(Zend_Date::ISO_8601);
}
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.date.additional.timezones"></a>9.7.3. Timezones</h3></div></div></div>
<p>
            Timezones are as important as dates themselves. There are several timezones depending on where
            in the world a user lives. So working with dates also means to set the proper timezone.
            This may sound complicated but it's easier as expected. As already mentioned in the first chapter
            of <code class="code">Zend_Date</code> the default timezone has to be set. Either by <code class="code">php.ini</code> or
            by definition within the bootstrap file.
        </p>
<p>
            A <code class="code">Zend_Date</code> object of course also stores the actual timezone. Even if the timezone
            is changed after the creation of the object it remembers the original timezone and works with it.
            It is also not necessary to change the timezone within the code with php functions.
            <code class="code">Zend_Date</code> has two built-in functions which makes it possible to handle this.
        </p>
<p>
            <code class="code">getTimezone()</code> returns the actual set timezone of within the <code class="code">Zend_Date</code>
            object. Remember that <code class="code">Zend_Date</code> is not coupled with php internals. So the returned
            timezone is not the timezone of the php script but the timezone of the object.
            <code class="code">setTimezone($zone)</code> is the second function and makes it possible to set new timezone for
            <code class="code">Zend_Date</code>. A given timezone is always checked. If it does not exist an exception will
            be thrown. Additionally the actual scripts or systems timezone can be set to the date object by
            calling  <code class="code">setTimezone()</code> without the zone parameter. This is also done automatically
            when the date object is created.
        </p>
<div class="example">
<a name="zend.date.additional.timezones.example-1"></a><p class="title"><b>Example 9.21. Working with timezones</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

// Set a default timezone... this has to be done within the bootstrap file or php.ini
// We do this here just for having a complete example
date_default_timezone_set('Europe/Vienna');

// create a date object
$date = new Zend_Date('10.03.2007', Zend_Date::DATES, 'de');

// view our date object
print $date-&gt;getIso();

// what timezone do we have ?
print $date-&gt;getTimezone();

// set another timezone
$date-&gt;setTimezone('America/Chicago');

// what timezone do we now have ?
print $date-&gt;getTimezone();

// see the changed date object
print $date-&gt;getIso();
            </pre></div>
</div>
<br class="example-break"><p>
            <code class="code">Zend_Date</code> always takes the actual timezone for object creation as shown in the first
            lines of the example. Changing the timezone within the created object also has an effect to the
            date itself. Dates are always related to a timezone. Changing the timezone for a <code class="code">Zend_Date</code>
            object does not change the time of <code class="code">Zend_Date</code>. Remember that internally dates are always
            stored as timestamps and in GMT. So the timezone means how much hours should be substracted or added
            to get the actual global time for the own timezone and region.
        </p>
<p>
            Having the timezone coupled within <code class="code">Zend_Date</code> has another positive effect. It is possible
            to have several dates with different timezones.
        </p>
<div class="example">
<a name="zend.date.additional.timezones.example-2"></a><p class="title"><b>Example 9.22. Multiple timezones</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Date.php';

// Set a default timezone... this has to be done within the bootstrap file or php.ini
// We do this here just for having a complete example
date_default_timezone_set('Europe/Vienna');

// create a date object
$date = new Zend_Date('10.03.2007 00:00:00', Zend_Date::ISO_8601, 'de');

// view our date object
print $date-&gt;getIso();

// the date stays unchanged even after changeing the timezone
date_default_timezone_set('America/Chicago');
print $date-&gt;getIso();

$otherdate = clone $date;
$otherdate-&gt;setTimezone('Brazil/Acre');

// view our date object
print $otherdate-&gt;getIso();

// set the object to the actual systems timezone
$lastdate = clone $date;
$lastdate-&gt;setTimezone();

// view our date object
print $lastdate-&gt;getIso();
            </pre></div>
</div>
<br class="example-break">
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.date.constants.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.date.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.db.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">9.6. Constants for General Date Functions </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Chapter 10. Zend_Db</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
